package com.cb.web.wasf;

import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * @author CBO
 */
public class StringUtils {

    public static String blankToNull(String value) {
        return isBlank(value) ? null : value;
    }

    public static String emptyToNull(String value) {
        return isEmpty(value) ? null : value;
    }

    public static boolean isEmpty(String value) {
        return value == null || "".equals(value);
    }

    public static boolean isBlank(String value) {
        return value == null || isEmpty(value.trim());
    }

    public static String byteToBase64(byte[] data) throws UnsupportedEncodingException {
        try {
            byte[] decoded = Base64.encodeBase64(data);
            return new String(decoded, "UTF-8");
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException(uee);
        }
    }

    public static String hashSHA256(String password) {
        try {
            return byteToBase64(DigestUtils.sha256(password));
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException(uee);
        }
    }
}
